﻿/* 
 Copyright (C) 2011  Amblox Project

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;

using Amblox.Database;
using Amblox.Encoding;
using Amblox.Managers;

namespace Amblox.Handlers
{
    public partial class Handler
    {
        private bool LoginRegistered = false;
        private bool HabboInfoRegistered = false;

        public void HabboInfoRequest()
        {
            FuseBuilder InfoRequest = new FuseBuilder(5);
            InfoRequest.Append(Habbo.ID);
            InfoRequest.AppendBreak();
            InfoRequest.Append(Habbo.Name);
            InfoRequest.AppendBreak();
            InfoRequest.Append(Habbo.Look);
            InfoRequest.AppendBreak();
            InfoRequest.Append(Habbo.Gender);
            InfoRequest.AppendBreak();
            InfoRequest.Append(Habbo.RealName);
            InfoRequest.AppendBreak();
            InfoRequest.AppendWired(0);
            InfoRequest.AppendBreak();
            InfoRequest.AppendWired(0);
            InfoRequest.AppendWired(0);
            InfoRequest.AppendWired(0); //Rispetti ricevuti
            InfoRequest.AppendWired(0); //Rispetti rimanenti per gli habbo
            InfoRequest.AppendWired(0); //Rispetti rimanenti per gli animali
            Connection.SendData(InfoRequest);
        }

        public void HabboBalanceRequest()
        {
            FuseBuilder BalanceRequest = new FuseBuilder(6);
            BalanceRequest.Append(Convert.ToString(Habbo.Credits) + ".0");
            BalanceRequest.AppendChar(1);
            BalanceRequest.Initialize(438);
            BalanceRequest.AppendWired(Habbo.Pixels);
            BalanceRequest.AppendWired(0); // Notify Amount
            Connection.SendData(BalanceRequest);
        }

        public void HabboSubscriptionRequest()
        {

        }

        public void SSO_Login()
        {
            int length = JeaxEncoding.decodeB64(Packet.Substring(2, 2));
            string sso_ticket = Packet.Substring(4, length);

            int HabboID;

            using (DatabaseClient dbClient = Environment.dbManager.GetClient())
            {
                dbClient.AddParamWithValue("ticket", sso_ticket);

                if (!dbClient.FindsResult("SELECT id FROM users WHERE sso_ticket = @ticket"))
                    goto LoginFailed;

                HabboID = dbClient.ReadInt("SELECT id FROM users WHERE sso_ticket = @ticket");
            }

            if (HabboID == 0)
                goto LoginFailed;

            Habbo = HabboManager.GetHabbo(HabboID);
            Habbo.SetClient(Client);

            FuseBuilder LoginResponse = new FuseBuilder();

            LoginResponse.Append(Habbo.Fuselist.Serialize());

            LoginResponse.AppendChar(1);

            LoginResponse.Initialize(290);
            LoginResponse.AppendBoolean(true);
            LoginResponse.AppendBoolean(false);

            LoginResponse.AppendChar(1);

            LoginResponse.Initialize(3);

            LoginResponse.AppendChar(1);

            LoginResponse.Initialize(570);
            LoginResponse.AppendBoolean(true);

            Connection.SendData(LoginResponse);

            RegisterHabboInfo();
            RegisterMessenger();

            return;

        LoginFailed:
            Destroy();
        }

        public void RegisterLogin()
        {
            if (LoginRegistered)
                return;

            LoginRegistered = true;
            Packets.Add(415, new Request(SSO_Login));
        }

        public void RegisterHabboInfo()
        {
            if (HabboInfoRegistered)
                return;

            HabboInfoRegistered = true;

            Packets.Add(7, new Request(HabboInfoRequest));
            Packets.Add(8, new Request(HabboBalanceRequest));
            Packets.Add(26, new Request(HabboSubscriptionRequest));
        }
    }
}
